package com.wgz.flag.controller;

import com.wgz.flag.common.Result;
import com.wgz.flag.domain.dto.GroupMemberDTO;
import com.wgz.flag.domain.vo.GroupMemberVO;
import com.wgz.flag.service.GroupMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group/member")
@CrossOrigin(origins = {"http://localhost:8081","http://localhost:8082"}, allowCredentials = "true")
@RequiredArgsConstructor
public class GroupMemberController {

    private final GroupMemberService groupMemberService;

    /**
     * 获取群成员列表
     */
    @GetMapping("/list")
    public Result<List<GroupMemberVO>> getGroupMembers(@RequestParam Long groupId) {
        List<GroupMemberVO> members = groupMemberService.getGroupMembers(groupId);
        return Result.success(members);
    }

    /**
     * 根据用户ID获取群成员信息
     */
    @GetMapping("/getByUserId")
    public Result<GroupMemberVO> getGroupMember(@RequestParam Long groupId, @RequestParam Long userId) {
        GroupMemberVO member = groupMemberService.getGroupMember(groupId, userId);
        return Result.success(member);
    }

    /**
     * 踢出群成员
     */
    @DeleteMapping("/kick")
    public Result<String> kickMember(@RequestParam Long groupId, 
                                   @RequestParam Long operatorId, 
                                   @RequestParam Long targetId) {
        groupMemberService.kickMember(groupId, operatorId, targetId);
        return Result.success("已将该成员移出群聊");
    }

    /**
     *  退出群聊
     */
    @DeleteMapping("/exit")
    public Result<String> exitGroup(@RequestParam Long groupId, @RequestParam Long userId) {
        groupMemberService.kickMember(groupId, userId, userId);
        return Result.success("成功退出群聊");
    }

    /**
     * 禁言群成员
     */
    @PostMapping("/mute")
    public Result<String> muteMember(@RequestParam Long groupId, 
                                   @RequestParam Long operatorId, 
                                   @RequestParam Long targetId, 
                                   @RequestParam Integer duration) {
        groupMemberService.muteMember(groupId, operatorId, targetId, duration);
        return Result.success("禁言成功");
    }

    /**
     * 解除禁言
     */
    @PostMapping("/unmute")
    public Result<String> unmuteMember(@RequestParam Long groupId, 
                                     @RequestParam Long operatorId, 
                                     @RequestParam Long targetId) {
        groupMemberService.unmuteMember(groupId, operatorId, targetId);
        return Result.success("已解除禁言");
    }

    /**
     * 设置管理员
     */
    @PostMapping("/set-admin")
    public Result<String> setAdmin(@RequestParam Long groupId, 
                                 @RequestParam Long operatorId, 
                                 @RequestParam Long targetId) {
        groupMemberService.setAdmin(groupId, operatorId, targetId);
        return Result.success("已设置为管理员");
    }
}